<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');
/**
 * @author  Pwint Phyu Shwe <[email]>
 * This is the main controller of the default home page.
 * @date(12-05-2013)
 */
class Home extends CI_Controller {

	public function __construct() {
		parent::__construct();
		// load model
		$this->load->model('Classifiedscatg_model','',TRUE);
		$this->load->model('Classified_model','',TRUE);
		$this->load->model('Property_model','',TRUE);
		$this->load->model('Propertycatg_model','',TRUE);
		$this->load->model('Autotype_model','',TRUE);
		$this->load->model('Automobile_model','',TRUE);
		$this->load->model('Autobrand_model','',TRUE);	
		$this->load->model('Commercial_model','',TRUE);	
		$this->load->model('Blog_model','',TRUE);	
	}
	
	// load index file
	function index() {
		$data['title'] = 'May Kha Classifieds Portal';
		$data['message'] = '';
		$list = $this->_get_latest_ads();
		$data['active'] = 'home';
		//$this->load->view('/includes/header', $data);
		$this->load->view('pages/home.php', $list);
		//$this->load->view('/includes/footer');

	}
	/**
	* get latest ads (property, classifieds, automobiles) to display
	* get categories together with total numbers for 3 types
	**/
	function _get_latest_ads()
	{
		$info = array();
		$cond['is_active'] = 1;
		//$cond['promote_ad'] = 1;
		$sort =array( 
			array(
				'column'=>'posted_date',
				'order' => 'asc'
				),
			array(
				'column' => 'title',
				'order' => 'asc'
				),
		 );
		//classifeds
		$clist = $this->Classified_model->get_result($cond, 4, 0, $sort);
		$info['clist'] = $clist['rows'];
		//property
		$plist = $this->Property_model->get_result($cond, 3, 0, $sort);
		if (count($plist['rows']) > 0){
			$info['pspecial'] = $plist['rows'][0];
			unset($plist['rows'][0]);
		}
		$info['plist'] = $plist['rows'];

		//automobiles
		$alist = $this->Automobile_model->get_result($cond, 4, 0, $sort);
		$info['alist'] = $alist['rows'];
		//automobile brands
		$autoBrands = $this->Autobrand_model->get_autos_cag_total();
		$info['autoBrands'] = $autoBrands['rows'];
		//property types
		$propTypes = $this->Propertycatg_model->get_property_type_total();
		$info['propTypes'] = $propTypes['rows'];
		//buy/sell classifieds types
		$advTypes = $this->Classifiedscatg_model->get_cls_type_total();
		$info['advTypes'] = $advTypes['rows'];
		
		$catg = $this->Classifiedscatg_model->get_classifieds_categories();
		unset($catg['']);
		$info['catg'] = $catg;
		
		$hotsearches = $this->Classifiedscatg_model->get_random_category();
		$info['hotsearches'] = $hotsearches;

		$propcatg = $this->Propertycatg_model->get_property_categories();
		unset($propcatg['']);
		$info['propcatg'] = $propcatg;
		
		$vehcatg = $this->Autobrand_model->get_brands();
		unset($vehcatg['']);
		$info['vehcatg'] = $vehcatg;
		
		//articles
		$sort = array( 
			array(
				'column'=>'creation_date',
				'order' => 'desc'
				)
		 );
		$cond['is_active'] = 1;
		$articles = $this->Blog_model->get_all_entries($cond,$sort,5);
		$cond['is_active'] = 2;
		$feature_articles = $this->Blog_model->get_all_entries($cond, $sort, 2);
		$info['articles'] = $articles;
		$info['feature_articles'] = $feature_articles;
		//$info['news'] = array("A","B","C");
		$info['news'] = $this->_get_news();
		$info['commercials'] = $this->_get_commercials();
		return $info;
	}
	/** get commerical advertisements info **/
	function _get_commercials()
	{
		$results = $this->Commercial_model->get_list();

		return $results;
		
	}
	/**
	* Get Latest News from news.xml file
	**/
	function _get_news()
	{
        /**$filename = 'news'.'.txt';
        $textFile = base_url() . 'data/' . $filename;
		$textRaw = file_get_contents($textFile);
		log_message('DEBUG', 'File Content' . $textRaw);
        $news = $this->_read_file($textRaw);
        $results = array();
        foreach($news[1] as $new){
        		$results[] = $new;
        }**/
        $cond['adv_code'] = 'hot_news';
        $cond['is_active'] = 1;
        $results = $this->Commercial_model->get($cond);

        return $results;
		//$this->parser->parse('table_view', $data);
	}
	 function _getXML($fname)
    {
 
        $filename = $fname.'.xml';
		$xmlfile = base_url(). 'assets/data/' . $filename;
		log_message('DEBUG', 'XML Parser=>' . $filename);
        $xmlRaw = file_get_contents($xmlfile);

        $this->load->library('simplexml');
        $xmlData = $this->simplexml->xml_parse($xmlRaw);
		 $result[] = array();
        foreach($xmlData['new'] as $row)
        {
        	$data = $row['description'];
            $result[] = $data;
        }
         return $result;
	}

	function _read_file($textRaw)
	{
		
		//$pattern = "'<p class=\"review\">([^<]*)</p>'si";
		$pattern = "'<description>(.*?)<\/description>'";
		preg_match_all($pattern, $textRaw, $matches);

		
		return $matches;


	}
}
/* End of file home.php */
/* Location: ./application/contorller/home.php */
?>
